package org.NooLab.itexx;

import java.util.ArrayList;

import org.NooLab.itexx.app.up.IniProperties;
import org.NooLab.itexx.app.up.TxStartup;
import org.NooLab.itexx.co.CoWorkers;
import org.NooLab.itexx.co.targets.CoDocumentObserver;
import org.NooLab.structures.ITexxPublicIntf;

import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;

 
 
/**
 * 
 * 
 * 
 * http://stackoverflow.com/questions/6069878/re-launch-jvm-with-bigger-heap-space
 * 
 * see JPBdemo in utilities !!!!!!!!
 * 
 * 
 * 
 * The iTexxFactory
 * 
 * 
 * 
 *
 */
public class ITexxFactory {

	
	ITexxFactory itxFactory ;
	
	static ITexx iTexx ;

	private static ITexxFactory factory;

	private static int startevent=0;
	

	ArrayList<Integer> serverPorts = new ArrayList<Integer> ();
	
	boolean appIsRunning = false;

	private String systemRootDir;
	
	
	// ========================================================================
	private ITexxFactory() throws Exception{
		
		itxFactory = this;
		iTexx = new ITexx(this) ;
		 
	 
	}
	
	public static ITexxFactory getInstance() {
		
		ITexxFactory itx=null ;
		 
		try {
			
			if (factory==null){
				itx  =  new ITexxFactory();
				factory = itx; 
			}else{
				itx = factory ;
			}
			startevent++;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return itx ;
	}
	// ========================================================================


	public void close(){
		
		// message
		
		
		
		
		// finally the loop here:
		appIsRunning=false;
	}
	
	
	public ITexxPublicIntf getITexx(){
		
		return iTexx;
	}

	public static boolean projectSpaceExists(String psLabel) {
		 
		return false;
	}

	public static void completeProjectSpace() {
		 
		
	}

	public static void organizeRawProjectData() {
		 
		
	}

	public static void init(Class clzz) {

		String str ="";
		
		
		if (startevent>1){
			return;
		}
		// typically -1, as the installation comes as <project name>/bin/..jar
		try {
			
			new IniProperties();
			
			TxStartup.setApplicationID("itexxmain", clzz,-1);
			
			TxStartup.setProjectSpaceLabel();
			
			
			str = TxStartup.getProjectBasePath() ;
			
			IniProperties.setBinPath( "D:/data/iTexx/bin/");
			str = IniProperties.getFluidSomProjectBasePath() ;
			IniProperties.saveIniProperties();
			
			
			
		} catch (Exception e) {
			
			System.err.println("\nStarting iTexx failed ("+e.getMessage()+"). Stopping. \n");
			System.exit(-7);
		}
		
		str = str+ " ";
	}


	
	public static  void stop() {
		 
		// distribute the stopping messages...
		
		new FactoryStopper(iTexx);
		
		IniProperties.saveIniProperties();
		System.exit(0);
	}
	
	public ITexxFactory start(){
		
		if (startevent<=1){
			iTexx.start();
		}
		
		setRootDir( IniProperties.fluidSomProjectBasePath);
		
		String str ;
		str = TxStartup.getProjectBasePath()  ;
		str = IniProperties.lastProjectName ;
		 
		
		iTexx.createProcesses() ;
		
		
		return itxFactory;
	}

	private void setRootDir(String path) {
		 
		systemRootDir = path;
		
		iTexx.out.print(3, "root folder : "+path) ;
	}

	public String getSystemRootDir() {
		return systemRootDir;
	}

	/** only ports >1100 are allowed */
	public void addServerPort(int srvport) {
		
		if ((srvport>1100) && (serverPorts.indexOf(srvport)<0)){
			if (serverPorts.indexOf(srvport)<0){
				serverPorts.add(srvport);
			}
		}
	}

	public ArrayList<Integer> getServerPorts() {
		return serverPorts;
	}

	public void setServerPorts(ArrayList<Integer> serverPorts) {
		this.serverPorts = serverPorts;
	} 
 
}

class FactoryStopper{
	
	public FactoryStopper(ITexx iTexx){
		if (iTexx!=null){
			iTexx.close();
		}
	}

}
